package com.yoo.ons.base

import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.yoo.ons.base.adpater.BaseRecyclerAdapter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yoo.module_base.base.BaseViewModel


abstract class BaseVMListActivity<T, binding : ViewBinding, viewModel : BaseViewModel>(inflaterBlock: (LayoutInflater) -> binding, modelClass: Class<viewModel>) : BaseVMActivity<binding, viewModel>(inflaterBlock, modelClass){
    //初始页数
    protected var initialPage = 0

    protected var page = this.getInitPageIndex()
    //分页数量
    var pageSize = 10

//    override lateinit val helper: QuickAdapterHelper

    var adapter: BaseRecyclerAdapter<T, *> = this.getMyAdapter()

    /**
     * Adapter
     */
    abstract fun getMyAdapter(): BaseRecyclerAdapter<T, *>

    /**
     * 刷新控件
     */
    abstract fun getSmartRefreshLayout(): SmartRefreshLayout?

    /**
     * RecyclerView
     */
    abstract fun getRecyclerView(): RecyclerView?

    /**
     * 是否开启加载更多 默认开启
     */
    open fun isCanLoadMore() : Boolean{
        return true
    }

    open fun getInitPageIndex(): Int {
        return initialPage
    }

    open fun getLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(this)
    }

    override fun initView() {
        initRecyclerView()
    }

    /**
     * 下啦刷新数据/首页加载数据
     */
    abstract fun onRefresh()

    /**
     * 上啦加载更多数据
     */
    abstract fun onLoadMore()

    private fun initRecyclerView() {
        getRecyclerView()?.layoutManager = getLayoutManager()
        getRecyclerView()?.adapter = adapter
        getSmartRefreshLayout()?.setEnableFooterFollowWhenNoMoreData(true)
        getSmartRefreshLayout()?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                initialPage = 0
                onRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                onLoadMore()
            }
        })

    }

    open fun loadData(data: ArrayList<T>, isEndPage: Boolean? = null) {
        if (page == getInitPageIndex()){
            getSmartRefreshLayout()?.finishRefresh(100)
            adapter.setDataList(data)
        }else{
            getSmartRefreshLayout()?.finishLoadMore(100)
            adapter.addAll(data)
        }
        var canLoadMore = isCanLoadMore() && (isEndPage ?: (data.size == pageSize))
        if (isEndPage != null) {
            canLoadMore = !isEndPage
        }
//        if (canLoadMore) {
//            adapter.l
//            adapter.loadMoreModule?.loadMoreComplete()
//        } else {
//            adapter.loadMoreModule?.loadMoreEnd()
//        }
//        getSwipeRefreshLayout()?.isRefreshing = false
//        if (page == getInitPageIndex() && data.size == 0) {
//            if (isShowEmptyView()){
//                emptyView?.setError(emptyText())
//            }
//        }
    }
}